<?php

/*
 * Copyright (C) 2014 vlasta81
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Description of Session
 *
 * @author vlasta81
 */

namespace system;

final class session {

    public static function isstarted() {
        if (session_id() == '') : return FALSE;
        else : return TRUE;
        endif;
    }

    public static function set($key, $value) {
        if (Session::isstarted() == TRUE) : $_SESSION[$key] = $value;
        endif;
    }

    public static function get($key) {
        if (Session::isstarted() == TRUE) :
            if (isset($_SESSION[$key])) : return $_SESSION[$key];
            else : return NULL;
            endif;
        else : return NULL;
        endif;
    }

    public static function del($key) {
        if (Session::isstarted() == TRUE) :
            if (isset($_SESSION[$key])) : unset($_SESSION[$key]);
            else : return NULL;
            endif;
        else : return NULL;
        endif;
    }

    public static function startSession() {
        if (Session::isstarted() == FALSE) : session_start();
            if (!isset($_SESSION['safety'])) : 
                session_regenerate_id(TRUE); $_SESSION['safety'] = TRUE;
            endif;
            $_SESSION['sessionid'] = session_id();
        endif;
    }

    public static function destroySession() {
        if (Session::isstarted() == TRUE) : session_destroy();
        endif;
    }

}
